package com.xxcxcxcx.xc_moremob.common.event;

import com.xxcxcxcx.xc_moremob.Main;
import com.xxcxcxcx.xc_moremob.mixin.FireworkRocketEntityMixin;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.world.damagesource.IndirectEntityDamageSource;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.FireworkRocketEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class MobEvent {
    @SubscribeEvent(priority = EventPriority.LOWEST)
    public static void onAttackPlayerLevel(LivingAttackEvent event) {
        if(event.getSource() instanceof IndirectEntityDamageSource indirectEntityDamageSource&&event.getEntity() instanceof LivingEntity livingEntity){
            Entity firework =  indirectEntityDamageSource.getDirectEntity();
            if(firework instanceof FireworkRocketEntity fireworkRocketEntity){
                ItemStack itemstack = fireworkRocketEntity.getEntityData().get(((FireworkRocketItem)fireworkRocketEntity).getDataIdFireworksItem());
                CompoundTag compoundtag = itemstack.isEmpty() ? null : itemstack.getTagElement(Main.MOD_ID);
                for(int i=1;i<=32;i++){
                    if(compoundtag.contains(MobEffect.byId(i).getDescriptionId()+"Level")){
                        int time=compoundtag.getInt(MobEffect.byId(i).getDescriptionId()+"Time");
                        int level=compoundtag.getInt(MobEffect.byId(i).getDescriptionId()+"Level");
                        MobEffect mobEffect = MobEffect.byId(i);
                        livingEntity.addEffect(new MobEffectInstance(mobEffect,time,level));
                    }
                }
            }
        }
    }
}
